/**
 * Model wskaźnika, dziedziczy po observable, żeby informować widok o zmianach
 * swojego stanu (rodzaje zmian opisane w IndicatorConstans).
 */

package com.beans.indicator.model;

import com.beans.indicator.constans.IndicatorConstans;
import java.util.Observable;

/**
 *
 * @author Wojtek
 */
public class IndicatorModel extends Observable {

	private int minValue = 0, maxValue = 100;
	private int minCriticalValue = 20, maxCriticalValue = 90;
	private int value = 50;
        private int oldValue = 50;
	private String alarmSoundPath;


	public int getMinValue() {
            return minValue;
	}
	public void setMinValue(int minValue) {
            this.minValue = minValue;
            notifyRangeChanged();
	}
	public int getMaxValue() {
            return maxValue;
	}
	public void setMaxValue(int maxValue) {
            this.maxValue = maxValue;
            notifyRangeChanged();
	}
	public int getMinCriticalValue() {
            return minCriticalValue;
	}
	public void setMinCriticalValue(int minCriticalValue) {
            if (!((minCriticalValue < minValue) /*|| (minCriticalValue > maxValue)*/ || (minCriticalValue > maxCriticalValue))) {
                this.minCriticalValue = minCriticalValue;
            }
            notifyCriticalChanged();
	}
	public int getMaxCriticalValue() {
            return maxCriticalValue;
	}
	public void setMaxCriticalValue(int maxCriticalValue) {
            if (!((maxCriticalValue > maxValue) /*|| (maxCriticalValue < minValue)*/ || (maxCriticalValue < minCriticalValue))) {
                this.maxCriticalValue = maxCriticalValue;
            }
            notifyCriticalChanged();
	}
	public int getValue() {
            return value;
	}
	public void setValue(int value) {
            if ((value >= minValue) && (value <= maxValue)) {
                this.oldValue = this.value;
                this.value = value;
                if (isValueCritical() && !isOldValueCritical()) {
                    notifyCriticalValueIn();
                } else if (isOldValueCritical() && !isValueCritical()) {
                    notifyCriticalValueOut();
                }
            }
            notifyValueChanged();
	}
	public String getAlarmSoundPath() {
            return alarmSoundPath;
	}
	public void setAlarmSoundPath(String alarmSoundPath) {
            this.alarmSoundPath = alarmSoundPath;
	}

        public boolean isValueCritical() {
            return (value < minCriticalValue) || (value > maxCriticalValue);
        }

        private boolean isOldValueCritical() {
            return (oldValue < minCriticalValue) || (oldValue > maxCriticalValue);
        }

        public int getValuesCount() {
            return maxValue-minValue;
        }

        public int getAbsoluteValue(int v) {
            return v-minValue;
        }

        public int getRelativeValue(int v) {
            return v+minValue;
        }

        private void notifyValueChanged() {
            setChanged();
            notifyObservers(IndicatorConstans.VALUE_CHANGED);
        }

        private void notifyRangeChanged() {
            setChanged();
            notifyObservers(IndicatorConstans.RANGE_CHANGED);
        }

        private void notifyCriticalChanged() {
            setChanged();
            notifyObservers(IndicatorConstans.CRITICAL_CHANGED);
        }

        private void notifyCriticalValueIn() {
            setChanged();
            notifyObservers(IndicatorConstans.CRITICAL_IN);
        }

        private void notifyCriticalValueOut() {
            setChanged();
            notifyObservers(IndicatorConstans.CRITICAL_OUT);
        }
}
